package cn.wolfcode.consumer._04_delay;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {

    public static void main(String[] args) throws Exception {
        //1 创建消费者对象
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("consumer-demo");
        //2 设置名字服务的地址
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3 订阅主题
        defaultMQPushConsumer.subscribe("05-delay", "*");
        //4 做监听，监听topic里面是否有消息，如果有消息的话会消费信息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消息是:" + new String(messageExt.getBody()));
                    System.out.println("消息的id是:" + new String(messageExt.getMsgId()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5 启动消费者
        defaultMQPushConsumer.start();
    }
}
